package com.bjpowernode.crm.workbench.web.controller;

import com.bjpowernode.crm.commons.contants.Contants;
import com.bjpowernode.crm.commons.domain.ReturnObject;
import com.bjpowernode.crm.settings.domain.DicValue;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.settings.service.DicValueService;
import com.bjpowernode.crm.settings.service.UserService;
import com.bjpowernode.crm.workbench.domain.Tran;
import com.bjpowernode.crm.workbench.dto.TranAddingDto;
import com.bjpowernode.crm.workbench.service.CustomerService;
import com.bjpowernode.crm.workbench.service.TranHistoryService;
import com.bjpowernode.crm.workbench.service.TranRemarkService;
import com.bjpowernode.crm.workbench.service.TranService;
import lombok.Getter;
import lombok.Setter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.ResourceBundle;

/**
 * 处理交易相关请求
 *
 * @author 刘逸晖
 */
@Getter
@Setter
@RequestMapping
public class TranController {

    /**
     * 处理交易相关业务
     */
    private TranService tranService;

    /**
     * 处理交易历史记录相关业务
     */
    private TranHistoryService tranHistoryService;

    /**
     * 处理交易备注相关业务
     */
    private TranRemarkService tranRemarkService;

    /**
     * 处理字典值相关业务
     */
    private DicValueService dicValueService;

    /**
     * 处理用户相关业务
     */
    private UserService userService;

    /**
     * 处理客户相关业务
     */
    private CustomerService customerService;

    /**
     * 跳转到交易主页面
     *
     * @return 模型和试图
     */
    @GetMapping("/workbench/transaction/index.do")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("stageList", this.dicValueService.queryDicValueByTypeCode("stage"))
                .addObject("transactionTypeList", this.dicValueService.queryDicValueByTypeCode("transactionType"))
                .addObject("sourceList", this.dicValueService.queryDicValueByTypeCode("source"))
                .setViewName("workbench/transaction/index");

        return modelAndView;
    }

    /**
     * 跳转至新增页
     *
     * @return 模型和试图
     */
    @GetMapping("/workbench/transaction/toSave.do")
    public ModelAndView toSave() {
        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("userList", this.userService.queryAllUsers())
                .addObject("stageList", this.dicValueService.queryDicValueByTypeCode("stage"))
                .addObject("transactionTypeList", this.dicValueService.queryDicValueByTypeCode("transactionType"))
                .addObject("sourceList", this.dicValueService.queryDicValueByTypeCode("source"))
                .setViewName("workbench/transaction/save");

        return modelAndView;
    }

    /**
     * 实现查询线索状态的可能性
     *
     * @param stageValue 线索状态字典值
     * @return 可能性
     */
    @GetMapping("/workbench/transaction/getPossibilityByStage.do")
    @ResponseBody
    public String getPossibilityByStage(String stageValue) {
        //解析properties配置文件，根据阶段获取可能性
        ResourceBundle bundle = ResourceBundle.getBundle("possibility");
        return bundle.getString(stageValue);
    }

    /**
     * 搜索客户名称
     *
     * @param customerName 客户名称的关键词
     * @return 搜索出的客户名称列表
     */
    @GetMapping("/workbench/transaction/queryCustomerNameByName.do")
    @ResponseBody
    public List<String> queryCustomerNameByName(String customerName) {
        return this.customerService.queryNamesByKeyword(customerName);
    }

    /**
     * 创建交易
     *
     * @param tranAddingDto 交易参数
     * @param session       Http 会话
     * @return 结果
     */
    @PostMapping("/workbench/transaction/saveCreateTran.do")
    @ResponseBody
    public ReturnObject saveCreateTran(TranAddingDto tranAddingDto, HttpSession session) {
        this.tranService.addTran(tranAddingDto, (User) session.getAttribute(Contants.SESSION_USER));

        ReturnObject returnObject = new ReturnObject();

        returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
        returnObject.setMessage("恭喜，添加交易成功");

        return returnObject;
    }

    /**
     * 查看交易详情
     *
     * @param id 交易 id
     * @return 模型和试图
     */
    @GetMapping("/workbench/transaction/detailTran.do")
    public ModelAndView detailTran(String id) {
        Tran tran = this.tranService.queryTranById(id);

        //根据交易所处阶段名称查询可能性
        ResourceBundle bundle = ResourceBundle.getBundle("possibility");
        String possibility = bundle.getString(tran.getStage());
        tran.setPossibility(possibility);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("tran", tran)
                .addObject("remarkList", this.tranHistoryService.queryTranHistoriesByTranId(id))
                .addObject("historyList", this.tranHistoryService.queryTranHistoriesByTranId(id))
                .addObject("stageList", this.dicValueService.queryDicValueByTypeCode("stage"))
                .setViewName("workbench/transaction/detail");

        return modelAndView;
    }

}
